$(function () {
    init()
    $(document).on('click','.delbtn',function () {
        var li = $(this).closest("li")
        var id = li.find('.comid').text()
        console.log(id)
        $('.delebtn').click(function () {

            $.ajax({
                url:'/comment/delete?id='+id,
                type:'Get',
                dataType:"json",
                processData: false,
                contentType:false,
                success:function (res) {
                    if (res.code===200){
                        $.toast({
                            title: '通知',
                            subtitle: '1秒前',
                            content: "删除成功",
                            type: 'success',
                            delay: 1000,
                            pause_on_hover: false,

                        });
                        li.remove()
                        $('#delmodal').modal('hide')
                    }
                    else{
                        $.toast({
                            title: '通知',
                            subtitle: '1秒前',
                            content: res.msg,
                            type: 'error',
                            delay: 3000,
                            pause_on_hover: false
                        });
                        return;
                    }
                }
            })
            // 页面移除

        })


    })
    function init() {
        $.ajax({
            url: '/comment/list',
            type: 'Get',
            dataType: "json",
            processData: false,   // 不处理发送的数据
            contentType: false,   // 不设置Content-Type请求头
            success:
                function (res) {
                    if (res.code!==200){
                        $.toast({
                            title: '通知',
                            subtitle: '1秒前',
                            content: res.msg,
                            type: 'error',
                            delay: 3000,
                            pause_on_hover: false
                        });
                        return;
                    }
                    var data  = res.data
                    console.log(data)
                    xianshi(10,1,data)
                    var page = Math.ceil(data.length/10)
                    $('#pagination').pagination({
                        pageCount: page,
                        jump: true,
                        coping: true,
                        homePage: '首页',
                        endPage: '末页',
                        prevContent: '上页',
                        nextContent: '下页',
                        current:1,  //当前第几页
                        callback:function (api) {
                            xianshi(10,api.getCurrent(),data)
                        }
                    })
                }
        })


    }

})

function xianshi(num,page,data) {
    var page2 = (page-1)*10
        $('.commentlistli').children().remove()
        for (let i = 0; i < num; i++) {
            if(data[i+page2]){
                var $str = $(' <li><div class="comid" style="display: none">'+data[i+page2].id+'</div>\n' +
                    '<div class="commenttitle"><a href="/unAuthorize/article?id='+data[i+page2].articleId+'" title="点击进入文章">'+data[i+page2].title+'</a></div>\n' +
                    '<div class="commentdetil">\n' +
                    '<span class="commentid">'+data[i+page2].author+'</span><span> &nbsp;:</span>\n' +
                    '<span class="commentcontent">'+data[i+page2].content+'<button class="delbtn fas fa-trash" data-toggle="modal" data-target="#delmodal">删除</button></span>\n' +
                    '</div>\n' +
                    '</li>'
                )
                $('.commentlistli').append($str)
            }
        }
}